package com.cjst.controller.part;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.controller.base.BaseController;
import com.cjst.controller.sys.SysUserController;
import com.cjst.pojo.part.PartType;
import com.cjst.pojo.sys.SysOffice;
import com.cjst.pojo.sys.SysRole;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.MapUtils;
import com.cjst.sdp.util.ZTreeNode;
import com.cjst.service.part.PartTypeService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.CloneClass;
import com.cjst.util.UserPrincipalUtil;
import com.cjst.util.code.PageUtil;
import com.cjst.util.common.DateUtil;
import com.cjst.util.json.ResultObj;


@Controller
@RequestMapping("/part/partType")
public class PartTypeController extends BaseController{
	private static final Logger log = Logger.getLogger(PartTypeController.class);
	@Autowired
	private PartTypeService partTypeService;
	
	@Value("${file.path}")
	private String filePath;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
    //@RequiresPermissions("part_type")
    public String toPartType() {
        return "part/part_type/part_type";
    }
	
	/**
     * 跳转到添加页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @RequiresPermissions("part_type_add")
    public String addPartTyper() {
        return "part/part_type/part_type_add";
    }
	
	
	@RequestMapping(value = "/list")
    @ResponseBody
    public Object list(PartType partType) {
		/*
		if(StringUtils.isEmpty(partType.getPtypeParent())){
			return new ResultObj(1, "参数传递错误，查询失败", null);
		}
		
		WherePrams wherePrams = Method.where("delFlag", C.EQ, "0");
		
		wherePrams=wherePrams.and("ptypeParent", C.EQ, partType.getPtypeParent());
		List<PartType> result=partTypeService.list(wherePrams);
		*/
		return null;
	}
	
	
	@RequestMapping(value = "/listAll")
	@ResponseBody
	public Object list() {
		
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		
		WherePrams wherePrams = Method.where("delFlag", C.EQ, "0");

		UserPrincipalUtil.getPriceMegod(wherePrams, sysUser, null);
		
		PageUtil<PartType> pageUtil = partTypeService.listPageCount(wherePrams);
        List<PartType> partTypes = pageUtil.getData();
		
        List<Map<String, Object>> maps = new ArrayList<>();
        for (PartType partType : partTypes) {
            Map<String, Object> partTypeMap = MapUtils.transBean2Map(partType);
            maps.add(partTypeMap);
        }
        
        
        PageUtil<Map<String, Object>> pages = new PageUtil<>();

        CloneClass.getClone(pageUtil, pages);
        pages.setData(maps);
        return this.packForBT(pages);
	}
	
	
	@RequestMapping(value = "/save/addOne", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("part_type_add")
    public Object addOne(PartType partType, HttpServletRequest req) {
		
		
		
		
		if (StringUtils.isEmpty(partType.getPtypeParent())) {
			return new ResultObj(1, "参数传递错误，父类id不能为空 ,失败", null);
		}
		
		//
		if (StringUtils.isEmpty(partType.getPtypeName())) {
			return new ResultObj(1, "参数传递错误，设备名不能为空 ,失败", null);
		}
		
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		
		partType.setCreateBy(sysUser.getId());
		
		String flag=partTypeService.addLocalByid(partType);
		
		
		return new ResultObj(0, "保存成功", flag);
	}
	
	 /**
     * 部件类型详情
     * @param partTypeId
     * @param model
     * @return
     */
    @RequestMapping(value = "/view/{partTypeId}", method = RequestMethod.GET)
    @RequiresPermissions("part_type_view")
    public String viewUser(@PathVariable String partTypeId, Model model) {
        WherePrams where = Method.where("id", C.EQ, partTypeId);
        PartType partType = partTypeService.get(where);
        if (partType != null) {
            Map<String, Object> partTypeMap = MapUtils.transBean2Map(partType);
            model.addAttribute("partType", partTypeMap);
        }
        return "part/part_type/part_type_detail";
    }

	
	@RequestMapping(value = "/save/addMany")
    @ResponseBody
    public Object addMany(PartType partType) {
		
		
		return null;
	}
	
	@RequestMapping(value = "/delete/deleteOne")
    @ResponseBody
    public Object deleteOne(PartType partType) {
		
		
		return null;
	}
	
	@RequestMapping(value = "/save/delete")
    @ResponseBody
    @RequiresPermissions("part_type_delete")
    public Object saveDelete(String ids) {
		 if (!StringUtils.isNotEmpty(ids)) {
	            return new ResultObj(1, "参数传递错误", null);
	        }
	        String[] list = ids.split(",");
	        for (String id : list) {
	            //PartType partType2 = partTypeService.get(id);
	            WherePrams wherePrams = Method.where("id", C.EQ, id);
	            PartType partType1 = new PartType();
	            partType1.setDelFlag(1);
	            partTypeService.updateLocal(partType1, wherePrams);
	        }

	        return new ResultObj(0, "删除成功", null);
	}
	
	
	@RequestMapping(value = "/update/{partTypeId}", method = RequestMethod.GET)
	@RequiresPermissions("part_type_edit")
	public Object toUpdate(@PathVariable String partTypeId, Model model) {
		PartType partType = partTypeService.get(partTypeId);
		if(partType!=null){			//filePath
			partType.setFback(filePath+partType.getFback());
			String pid=partType.getPtypeParent();
			if(pid!=null && pid.length()>0){
				PartType ppartType = partTypeService.get(pid);
				if(ppartType!=null) {
					partType.setPtypeParent(ppartType.getPtypeName());
				}
			}
			
		}
		model.addAttribute("partType", partType);

		return "part/part_type/part_type_edit";
	}
	
	
	@RequestMapping(value = "/save/updateOne", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("part_type_edit")
    public Object updateOne(PartType partType) {
		if (partType == null) {
            return new ResultObj(1, "参数传递错误，修改失败", null);
        }
        WherePrams where = Method.where("id", C.EQ, partType.getId());
        PartType partType2 = partTypeService.get(where);
        if (partType2 == null) {
            return new ResultObj(1, "根据Id未查询到部件信息，修改失败", null);
        }
      
        /*
         * 
         * 往后需添加一系列修改规则
         * 
         */
        
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		
		partType.setUpdateBy(sysUser.getId());
        

        int flag = partTypeService.updateLocal(partType);
        //updateRedisUser();
        return new ResultObj(0, "修改成功", flag);
	}
	
	 /**
     * 点击父级编号出现菜单树形结构
     */
    @RequestMapping(value = "/selectMenuTreeList", method = RequestMethod.POST)
    @ResponseBody
    public List<ZTreeNode> selectMenuTreeList() {
        List<ZTreeNode> menuTreeList = partTypeService.tree();
        menuTreeList.add(ZTreeNode.createParent());
        return menuTreeList;
    }
	
}
